package com.bootdo.wx.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.bootdo.common.utils.PageHelperUtils;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.alishipin.InitVodClient;
import com.bootdo.wx.dao.GoodsGoodsDao;
import com.bootdo.wx.dao.GoodsProductDao;
import com.bootdo.wx.dao.LessonContentDao;
import com.bootdo.wx.dao.LessonDao;
import com.bootdo.wx.domain.GoodsGoodsDO;
import com.bootdo.wx.domain.GoodsProductDO;
import com.bootdo.wx.domain.LessonContentDO;
import com.bootdo.wx.domain.LessonDO;
import com.bootdo.wx.service.LessonService;
import com.github.pagehelper.PageInfo;



@Service
public class LessonServiceImpl implements LessonService {
	@Autowired
	private LessonDao lessonDao;
	@Autowired
	private LessonContentDao lessonContentDao;
	@Autowired
	private GoodsGoodsDao goodsGoodsDao;
	@Autowired
	private GoodsProductDao goodsProductDao;
	
	@Override
	public LessonDO get(String id){
		return lessonDao.get(id);
	}
	
	@Override
	public List<LessonDO> list(Map<String, Object> map){
		return lessonDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return lessonDao.count(map);
	}
	
	@Override
	public int save(LessonDO lesson){
		return lessonDao.save(lesson);
	}
	
	@Override
	public int update(LessonDO lesson){
		return lessonDao.update(lesson);
	}
	
	@Override
	public int remove(String id){
		return lessonDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return lessonDao.batchRemove(ids);
	}

	@Override
	public List<LessonDO> likeList(Map<String, Object> map) {
		
		return lessonDao.likeList(map);
	}

	@Override
	public R getRecommend(Map<String, Object> map) {
		if("0".equals(map.get("type").toString())||"1".equals(map.get("type").toString())){
			//视频
			PageHelperUtils.page(map);
			List<Map<String, Object>> lessonList = lessonDao.getRecommend(map);
			PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String, Object>>(lessonList);
			return R.ok().put("data", pageInfo);
		}else{
			//文章
			PageHelperUtils.page(map);
			List<Map<String, Object>> lessonContentList = lessonContentDao.getRecommend();
			PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String, Object>>(lessonContentList);
			return R.ok().put("data", pageInfo);
		}
	}

	@Override
	public R getLessonListByClassId(Map<String, Object> map) {
		if("0".equals(map.get("type").toString())){
			//视频
			PageHelperUtils.page(map);
			List<Map<String, Object>> lessonList = lessonDao.getLessonListByClassId(map.get("classId").toString());
			PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String, Object>>(lessonList);
			return R.ok().put("data", pageInfo);
		}else{
			//文章
			PageHelperUtils.page(map);
			List<Map<String, Object>> lessonContentList = lessonContentDao.getLessonContentListByClassId(map.get("classId").toString());
			PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String, Object>>(lessonContentList);
			return R.ok().put("data", pageInfo);
		}
	}

	@Override
	public R getLessonInfo(Map<String, Object> map) {
		if("0".equals(map.get("type").toString())){
			//视频
			try {
				LessonDO lesson = lessonDao.get(map.get("id").toString());
				lesson.setNumber(lesson.getNumber()+1);
				lessonDao.update(lesson);
				DefaultAcsClient client = InitVodClient.initVodClient();
				GetPlayInfoResponse response = new GetPlayInfoResponse();

				response = InitVodClient.getPlayInfo(client,lesson.getLessonId());
				List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
				//播放地址
				lesson.setLessonId(playInfoList.get(0).getPlayURL());
				/*for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
				   System.out.print("PlayInfo.PlayURL = " + playInfo.getPlayURL() + "\n");
				   lesson.setLessonId(playInfo.getPlayURL());
				}*/
				return R.ok().put("data", lesson);
			} catch (Exception e) {
				e.printStackTrace();
				return R.error();
			}
		}else{
			//文章
			LessonContentDO lessonContent = lessonContentDao.get(map.get("id").toString());
			lessonContent.setNumber(lessonContent.getNumber()+1);
			lessonContentDao.update(lessonContent);
			//关联的商品
			GoodsGoodsDO goodsGoodsDO = goodsGoodsDao.get(lessonContent.getGoodsId());
			Map<String, Object> returnMap = new HashMap<>();
			returnMap.put("lessonContent", lessonContent);
			if(goodsGoodsDO!=null){
				GoodsProductDO isDefaultByGoods = goodsProductDao.getIsDefaultByGoods(goodsGoodsDO.getId());
				returnMap.put("imgurl", goodsGoodsDO.getImgurl());
				returnMap.put("name", goodsGoodsDO.getName());
				returnMap.put("price", isDefaultByGoods.getPrice());
				returnMap.put("spec", isDefaultByGoods.getSpec());
			}
			return R.ok().put("data", returnMap);
		}
	}
	
}
